/**
 * 
 */
package com.base.myproject.client.busi.b2c;



import java.util.LinkedList;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.busi.b2c.b2cgcodeattr.AttrFactory;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.ListView;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.ui.Widget;

/**
 * 曾经浏览过的图片
 * @author zgw
 *2009-8-5 上午10:53:17
 * javazgw@gmail.com
 */
public class B2cgcodeYouSaw {
	String value=null;
	
	LinkedList<String> stack = new java.util.LinkedList<String>();
	final static int size = 5;
	static B2cgcodeYouSaw b2cgcodeyousaw = null;
	private B2cgcodeYouSaw()
	{
		//Cookies.setCookie(name, value)
//		java.util.ArrayList<String> al = new ArrayList<String>();
		
		init();
	}
	
	public static B2cgcodeYouSaw getinstance ()
	{
		if(b2cgcodeyousaw==null)
		{
			b2cgcodeyousaw = new B2cgcodeYouSaw();
			
		}
		return b2cgcodeyousaw;
		
	}
	
	void init()
	{
		//stack.setSize(size);
		
		value = Cookies.getCookie("b2cgcodeyousaw");
		
		String b2cgcodes = "''";
		if(value!=null)
		{
		String[] valuearray = value.split(",");
		for(String str:valuearray)
		{
			stack.addFirst(str);
			b2cgcodes +=",'"+str+"'";
		}
		MainPage.getRightpanel2().removeAll();
		MainPage.getRightpanel2().add(setRecommendGcode(b2cgcodes));
		MainPage.getRightpanel2().layout();
		}
		
	}
	
	public void add(String value)
	{
		if(!stack.contains(value))
		{
		
		stack.addFirst(value);
		
		while(stack.size()>size)
		{
			stack.removeLast();
		}
		String values = "";
		String b2cgcodes = "''";
		for(String str :stack)
		{
			values = str +","+values;
			b2cgcodes +=",'"+str+"'";
		}
		this.value =values;
		Cookies.setCookie("b2cgcodeyousaw",this.value);
		
			
		MainPage.getRightpanel2().removeAll();
		MainPage.getRightpanel2().add(setRecommendGcode(b2cgcodes));
		MainPage.getRightpanel2().layout();
		}
		
	}
	public java.util.LinkedList<String> getStackll()
	{
		return stack;
		
	}
	private Widget setRecommendGcode(String b2cgcodes)
	{
		
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("b2cgcode");
		type.addField("b2cgname");
		type.addField("gcode");
		type.addField("gname");

		type.addField("mainimagepath");
		type.addField("detailimageidx");
		StoreInfo info = new StoreInfo();

		info.setTablename("b2cgcodeattr");
		
		info.setSql("select top 3 b2cgcode,b2cgname,gcode,gname, mainimagepath,detailimageidx from b2cgcode where b2cgcode in ("+b2cgcodes+")");
	//	info.setParent(gcodestore);// 关联主表
		//info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
		//info.setLinkparentcolumns(new String[] { "gcode" }); // 关联主表字段
		//info.setKeyname(new String[] { "gcode", "idx" });
		BaseStore<JDBCModelData> imagestore = new BaseStore<JDBCModelData>(type, info);
		
		ListView view = new ListView<BaseModelData>();
		view.setId("img-chooser-view");  
	    view.setAutoHeight(true);  
	    view.setTemplate(getTemplate());  
	    
	    view.setStore(imagestore);  
	   view.getSelectionModel().addSelectionChangedListener(
				new SelectionChangedListener() {

					@Override
					public void selectionChanged(SelectionChangedEvent se) {
						// TODO Auto-generated method stub
						if (se.getSelection().size() > 0) {
							AttrFactory af = new AttrFactory(((BaseModelData)se.getSelection().get(0)).get("b2cgcode").toString());
							af.initAttrShow();
							OneProduct oneproduct = new OneProduct(af.getP());
							
//							oneproduct = new OneProduct(p);

							MainPage.getCenterpanel().removeAll();
							
				
							MainPage.getCenterpanel().add(oneproduct);
				

							MainPage.getCenterpanel().layout();
						}
					}
				});
	  
	    view.setBorders(false);
	    view.setItemSelector("div.thumb-wrap");  
	    ContentPanel cp = new ContentPanel();
	    cp.addStyleName("x-panel-backcolor");
	    cp.setStyleAttribute("border-color", "#FFCC00");
//	    System.out.println("---------"+cp.gee);
//	    cp.getElement("bwrap").setAttribute("border-color", "#FFCC00");
	    cp.add(view);
	    cp.setWidth(120);
	    cp.setHeading("最近浏览商品");
	    cp.addStyleName("basepanelmargin");
	    return cp;
		
	}
	protected native String getTemplate() /*-{
		

//	var r= ['<tpl  for=".">', 
//
//	'<div class="thumb-wrap"  id="{b2cgcode}" style="border: 0px solid white;width:110px;height:110px;margin: 10px 10px 10px 10px">', 
//
//	'<div class="thumb"  ><img style="width:100px;height:100px;border:0px double #eee;margin: 1px 1px 1px 1px" src="{mainimagepath}" title="{b2cgname}{#}"></div>', 
//
//
//	'<span class="x-editable">{b2cgcode}</span></div>', 
//	'</tpl>',
//	'<div class="x-clear"></div>'].join(""); 
var r2= ['<table width="50" border="0px"><tpl  for=".">', 
			'<tpl if="xindex %1 ==0">',
		'<tr>',
		'</tpl>',
		'<td >',
	'<div class="thumb-wrap"  id="{b2cgcode}" style="border: 0px solid white;width:100px;height:100px;margin: 1px 1px 1px 1px;">', 

	'<div class="thumb"  ><img style="width:100px;height:100px;border:0px double #eee;margin: 1px 1px 1px 1px" src="{mainimagepath}" title="a{b2cgname}{#}"></div>', 


	'<span class="x-editable">{b2cgname}</span></div>', 
	'<td>',
		'<tpl if="xindex %1 ==0">',
		'</tr>',
		'</tpl>',
	'</tpl></table>',
	'<div class="x-clear"></div>'].join(""); 
	return r2;
	//     return '<img src="http://www.google.cn/intl/zh-CN/images/logo_cn.gif" title="{name}"><img src="http://www.google.cn/intl/zh-CN/images/logo_cn.gif" title="{name}">';
}-*/;
}
